//
//  Kratos_AppleIPA_Kra.m
//  kk_framework
//
//  Created by 7yao.top on 2018/11/24.
//  Copyright © 2018年 7yao.top. All rights reserved.
//

#import "Kratos_AppleIAP_Kra.h"
#import "RMStore.h"

@interface Kratos_AppleIAP_Kra()
@property (nonatomic,strong)UIViewController *vc;
@end


@implementation Kratos_AppleIAP_Kra
static id _instance = nil;






+ (instancetype)Kratos_defaultStore_Kra
{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}


- (void)Kratos_PurchaseProduct:(NSString *)productID  Kratos_Success:(void (^)(NSString * invalidProductIdentifiers))success Kratos_failure:(void (^)(NSString *errormsg))failure
{
    
    if (productID.length == 0) {
        failure(@"产品ID不能为空");
        return;
    }
    
   
    NSArray *products = @[productID];
    
    [[RMStore defaultStore] requestProducts:[NSSet setWithArray:products] success:^(NSArray *products, NSArray *invalidProductIdentifiers) {
        
        if([products count] == 0){
            NSLog(@"--------------没有商品------------------");
            failure(@"没有商品");
            return;
        }
        
      
        
            [[RMStore defaultStore] addPayment:productID success:^(SKPaymentTransaction *transaction) {
                NSLog(@"交易成功");
                success(productID);
                
            } failure:^(SKPaymentTransaction *transaction, NSError *error) {
                NSLog(@"交易失败");
                failure(error.localizedDescription);
            }];
        
        
    
        
    } failure:^(NSError *error) {
        NSLog(@"--------------请求商品失败------------------");
        failure(@"请求商品失败");
    }];
}
@end
